sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/model/json/JSONModel"
],
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller,JSONModel) {
        "use strict";

        return Controller.extend("tabledel.controller.View1", {
            onInit: function () {
                var json = {
                    "Matnr": '1111',
                    "Maktx": '2222',
                    "Meins": '3333',
                }

                var oModel = new JSONModel(json);
                this.getView().setModel(oModel, "crt");

            },
            onAddItem: function () {
                var oBinding = this.byId("itemTable").getBinding("items")
                var count = oBinding.getCount() + 1

                oBinding.create({
                    CategoryID: count
                });
            },
            onDelItem: function () {
                var that = this
                var oTable = this.byId("itemTable")
                var sItems = oTable.getSelectedItems()
                var oBinding = that.byId("itemTable").getBinding("items")
                var contexts = oBinding.getContexts()
                // contexts.splice(1, 1)
                // oBinding.getContexts().splice(1, 1)
                var oModel = that.getView().getModel()
                // console.log(oModel.oData)
                // debugger




                // 선택된 아이템들의 컨텍스트를 추출합니다.
                var aContextsToDelete = sItems.map(function (oItem) {
                    return oItem.getBindingContext().getPath().replace("/","");
                    // return oItem.getBindingContext()
                });
                // debugger
                aContextsToDelete.forEach(oContext => {
                    oContext.delete()
                    //    console.log(oContext)
                    delete that.getView().getModel().oData[oContext];
                    oBinding.refresh();
                })

                // debugger
                // oTable.setShowOverlay(false);
                // console.log(oModel.oData)
                // oBinding.refresh();
                // console.log(oModel.oData)
          
            }
        });
    });
